import axios from "axios";
import { ElMessageBox, ElMessage } from "element-plus";
axios.defaults.headers["Content-Type"] = "application/json;charset=utf-8";
// 创建axios实例
const service = axios.create({
  baseURL: "/api",
  // 超时
  timeout: 1000 * 60 * 60 * 24,
  withCredentials: true
});
service.defaults.withCredentials = true
// request拦截器
service.interceptors.request.use(
  (config) => {
   // document.cookie = "currentOrg=current_org1446784640291962881; power-token=262a0ff9-e2be-4ed2-87fa-280ba9719524; Tenant-Id=1";
    // document.cookie = "currentOrg=current_org1446784640291962881";
    // document.cookie = "power-token=c238f6fa-befc-4470-80e8-c769aac42247";
    // document.cookie = "Tenant-Id=1";
    // config.headers["Cookie"] = "currentOrg=current_org1446784640291962881; power-token=262a0ff9-e2be-4ed2-87fa-280ba9719524; Tenant-Id=1";
    config.headers["Authorization"] = "eyJhbGciOiJIUzUxMiJ9.eyJ1c2VySWQiOjEzMzk1NTA0Njc5Mzk2MzkyOTksImFjY291bnQiOiJhZG1pbiIsInV1aWQiOiJhZWFmZmViNC1iNWNhLTRkYjgtYjQ4Ny1lNTgwMjAzNDgwZWUiLCJyZW1lbWJlck1lIjpmYWxzZSwiZXhwaXJhdGlvbkRhdGUiOjE3MTM3ODk4OTQzMjQsImNhVG9rZW4iOm51bGwsInRlbmFudENvZGUiOm51bGwsIm90aGVycyI6bnVsbCwic3ViIjoiMTMzOTU1MDQ2NzkzOTYzOTI5OSIsImlhdCI6MTcxMzE4NTA5NCwiZXhwIjoxNzEzNzg5ODk0fQ.-dN_KFjqM1wFWA09kG7xXFYheIJlifYnnIn7jtV4tawdalQt-aZ2ubaIUH6XqCz6OH6IIfrjzWWKN8E6j6bX9w";
    return config;
  },
  (error) => {
    console.log(error);
    Promise.reject(error);
  }
);
// 响应拦截器
service.interceptors.response.use(
  (res) => {
    const data = res.data;
    if (data.status === 401) {
      ElMessageBox.alert("登录状态已过期，请重新登录", "系统提示", {
        confirmButtonText: "重新登录",
        callback: () => {
          //  toLogin();
        },
      });
    } else if (data.status < 200 || data.status > 300) {
      ElMessage({
        message: data.message || "Error",
        type: "error",
        duration: 5 * 1000,
      });
      return Promise.reject(new Error(data.message || "Error"));
    } else if (data.status == 404) {
      ElMessage({
        message: "没有找到服务404",
        type: "error",
        duration: 5 * 1000,
      });
      return Promise.reject(new Error(data.message || "Error"));
    } else {
      return res;
    }
  },
  (error) => {
    const response = error.response;
    if (response) {
      if (response.data.code === 401) {
        ElMessageBox.alert("登录状态已过期，请重新登录", "系统提示", {
          confirmButtonText: "重新登录",
          callback: () => {
            // toLogin();
          },
        });
      } else if (response.data.code == 404) {
        ElMessage({
          message: `404 ${response.data.data.error} [${response.data.data.path}]`,
          type: "error",
          duration: 5 * 1000,
        });
      } else {
        console.log("err" + error);
        ElMessage({
          message: response.data.message,
          type: "error",
          duration: 5 * 1000,
        });
      }
    } else {
      ElMessage({
        message: error.message,
        type: "error",
        duration: 5 * 1000,
      });
    }
    return Promise.reject(error);
  }
);
export default service;
